#include "stdafx.h"
#include "Char.h"

Char::Char()
{
	m_nAccountId = 0;
	//m_nLoginTimes = 0;
	m_nRace = 0;
	m_nGender = 0;
	m_strCharName = "";
	m_nIngot = 0;
	m_nMoney = 0;
	m_nLevel = 0;
	m_nValue = 0;
	m_nImageId = 0;
	m_nMapId = 0;
	m_nMapX = 0;
	m_nMapY = 0;

	m_tLastVisitTime = 0;
	m_bOnline = false;
	for (Int32 i = 0; i < CHAR_INFO_SIZE; i++)
	{
		m_arCharInfoLoaded[i] = false;
	}
	m_nTimerSaveOfflineCharInfo = 0;
}

bool Char::IsOfflineCharInfoLoaded()
{
	return m_arCharInfoLoaded[CHAR_CHAR_INFO] && m_arCharInfoLoaded[CHAR_TREND_INFO] && m_arCharInfoLoaded[CHAR_MESSAGE_INFO];
}
bool Char::IsFullCharInfoLoaded()
{
	for (Int32 i = 0; i < CHAR_INFO_SIZE; i++)
	{
		if(!m_arCharInfoLoaded[i] )
			return false;
	}
	return true;
}
Char::Char(UInt32 accountId)
{
	m_nAccountId = accountId;
	m_nRace = 0;
	m_nGender = 0;
	m_strCharName = "";
	m_nIngot = 0;
	m_nMoney = 0;
	m_nLevel = 0;
	m_nValue = 0;
	m_nImageId = 0;
	m_nMapId = 0;
	m_nMapX = 0;
	m_nMapY = 0;

	m_tLastVisitTime = 0;
	m_bOnline = false;
	for (Int32 i = 0; i < CHAR_INFO_SIZE; i++)
	{
		m_arCharInfoLoaded[i] = false;
	}
	m_nTimerSaveOfflineCharInfo = 0;
}
Char::~Char()
{
	if(m_nTimerSaveOfflineCharInfo != 0)
		saveOfflineCharInfo();
	DestroyTimer(m_nTimerSaveOfflineCharInfo);
	LOGGER_INFO("~Char,m_nAccountId:%d ",m_nAccountId);
}
void Char::clearOnlineCharInfo()
{
	m_blkItemInfo.Clear(true);
	m_blkTaskInfo.Clear(true);
	m_blkFriendInfo.Clear(true);
	m_blkFlagInfo.Clear(true);

	m_arCharInfoLoaded[CHAR_ITEM_INFO] = false;
	m_arCharInfoLoaded[CHAR_TASK_INFO] = false;
	m_arCharInfoLoaded[CHAR_FRIEND_INFO] = false;
	m_arCharInfoLoaded[CHAR_FLAG_INFO] = false;
}

void Char::OnTimerSaveOfflineCharInfo(UInt32 nTimerId)
{
	if(m_nTimerSaveOfflineCharInfo != nTimerId)
	{
		LOGGER_ERROR("Char::OnTimerSaveOfflineCharInfo,m_nTimerSaveOfflineCharInfo:%d,nTimerId:%d",m_nTimerSaveOfflineCharInfo,nTimerId);
		DestroyTimer(nTimerId);
	}
	DestroyTimer(m_nTimerSaveOfflineCharInfo);
	saveOfflineCharInfo();
}
void Char::saveOfflineCharInfo()
{
	LOGGER_INFO("Char::saveOfflineCharInfo,m_nAccountId:%d",m_nAccountId);
	singleton<CharManager>::instance().saveOfflineCharInfo(m_nAccountId);
}
void Char::saveFullCharInfo()
{
	LOGGER_INFO("Char::saveFullCharInfo,m_nAccountId:%d",m_nAccountId);
	singleton<CharManager>::instance().saveFullCharInfo(m_nAccountId);
}